{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/tiago/anaconda3/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88\n",
      "  return f(*args, **kwds)\n"
     ]
    }
   ],
   "source": [
    "from collections import defaultdict\n",
    "import re\n",
    "import HTSeq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "lct_bed = HTSeq.BED_Reader('LCT.bed')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "defaultdict(<class 'int'>, {'ENSE': 27, 'NM': 17, 'CCDS': 17})\n"
     ]
    }
   ],
   "source": [
    "feature_types = defaultdict(int)\n",
    "\n",
    "for rec in lct_bed:\n",
    "    last_rec = rec\n",
    "    feature_types[re.search('([A-Z]+)', rec.name).group(0)] += 1\n",
    "\n",
    "print(feature_types)\n",
    "\n",
    "#Code specific to this dataset, document"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<GenomicFeature: BED line 'CCDS2178.11' at 2: 135788543 -> 135788322 (strand '-')>\n",
      "CCDS2178.11\n",
      "<class 'HTSeq.GenomicFeature'>\n",
      "2:[135788323,135788544)/-\n",
      "<class 'HTSeq._HTSeq.GenomicInterval'>\n"
     ]
    }
   ],
   "source": [
    "print(last_rec)\n",
    "print(last_rec.name)\n",
    "print(type(last_rec))\n",
    "interval = last_rec.iv\n",
    "print(interval)\n",
    "print(type(interval))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 135788323 135788544\n",
      "-\n",
      "221\n",
      "135788543\n",
      "2:135788323/-\n",
      "<class 'HTSeq._HTSeq.GenomicPosition'>\n"
     ]
    }
   ],
   "source": [
    "print(interval.chrom, interval.start, interval.end)\n",
    "print(interval.strand)\n",
    "print(interval.length)\n",
    "print(interval.start_d)\n",
    "print(interval.start_as_pos)\n",
    "print(type(interval.start_as_pos))\n",
    "\n",
    "#talk about overlaps\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Num exons: 17 / Begin: 135788323 / End 135837169\n",
      "Smaller exon: 79 / Larger exon: 1551 / Mean size: 340.2\n"
     ]
    }
   ],
   "source": [
    "exon_start = None\n",
    "exon_end = None\n",
    "sizes = []\n",
    "for rec in lct_bed:\n",
    "    if not rec.name.startswith('CCDS'):\n",
    "        continue\n",
    "    interval = rec.iv\n",
    "    exon_start = min(interval.start, exon_start or interval.start)\n",
    "    exon_end = max(interval.length, exon_end or interval.end)\n",
    "    sizes.append(interval.length)\n",
    "sizes.sort()\n",
    "print(\"Num exons: %d / Begin: %d / End %d\" % (len(sizes), exon_start, exon_end))\n",
    "print(\"Smaller exon: %d / Larger exon: %d / Mean size: %.1f\" % (sizes[0], sizes[-1], sum(sizes)/len(sizes)))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
